package com.platform.server.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.platform.server.dao.UserDao;
import com.platform.server.service.MessageService;
import com.platform.server.service.UserService;
import com.platform.server.util.AuroraPush;

@Component("AutoTask")
public class AutoTask {
	
	@Autowired
	private MessageService messageService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private UserDao userDao;
	
	/**
	 * 时间调度  每天中午12点执行
	 */
	@Scheduled(cron = "0 0 10 * * ?")
	public void selectMessageTime(){
		List<Map<String, Object>> lists = messageService.selectMessageList(new HashMap<>());
		for(int i = 0;i < lists.size();i++){
			if(lists.get(i).get("status").equals("待发送")){
				Date date = (Date) lists.get(i).get("push_time");
				long time = date.getTime();
				long nowTime = System.currentTimeMillis();
				if(nowTime > time){
					//修改消息状态
					int updateMessageStatus = messageService.updateMessageStatus((Integer)lists.get(i).get("id"));
					String scope = lists.get(i).get("scope").toString();
					if(scope.contains("全部")){
						//查看所有用户
						List<Map<String, Object>> userList = userService.selectUserList(null, null,null,null,null,null,null, null, null, null);
						for(int k = 0;k < userList.size();k++){
							//AuroraPush.pushTargetForDriver(userList.get(k).get("id").toString(),lists.get(i).get("title").toString());
							//添加为系统消息
							int f = messageService.insertSystemMessage(userList.get(k).get("id").toString(),lists.get(i).get("title").toString(), 
									lists.get(i).get("content").toString());
						}
					}
					if(scope.contains("VIP用户")){
						//查看所有vip用户
						List<Map<String, Object>> userList = userDao.selectAllVipUser();
						for(int k = 0;k < userList.size();k++){
							AuroraPush.pushTargetForDriver(userList.get(k).get("id").toString(),lists.get(i).get("title").toString());
							//添加为系统消息
							int f = messageService.insertSystemMessage(userList.get(k).get("id").toString(),lists.get(i).get("title").toString(), 
									lists.get(i).get("content").toString());
						}
					}
					if(scope.contains("非vip用户")){
						//查看所有非vip用户
						List<Map<String, Object>> userList = userDao.selectNotVipUser();
						for(int k = 0;k < userList.size();k++){
							//AuroraPush.pushTargetForDriver(userList.get(k).get("id").toString(),lists.get(i).get("title").toString());
							//添加为系统消息
							int f = messageService.insertSystemMessage(userList.get(k).get("id").toString(), lists.get(i).get("title").toString(), 
									lists.get(i).get("content").toString());
						}
					}
					if(scope.contains("普通会员")){
						//查看会员类别列表
						List<Map<String, Object>> userList = userDao.selectRankUser("普通会员");
						for(int k = 0;k < userList.size();k++){
							//AuroraPush.pushTargetForDriver(userList.get(k).get("id").toString(),lists.get(i).get("title").toString());
							//添加为系统消息
							int f = messageService.insertSystemMessage(userList.get(k).get("id").toString(), lists.get(i).get("title").toString(), 
									lists.get(i).get("content").toString());
						}
					}
					if(scope.contains("铜牌会员")){
						//查看会员类别列表
						List<Map<String, Object>> userList = userDao.selectRankUser("铜牌会员");
						for(int k = 0;k < userList.size();k++){
							//AuroraPush.pushTargetForDriver(userList.get(k).get("id").toString(),lists.get(i).get("title").toString());
							//添加为系统消息
							int f = messageService.insertSystemMessage(userList.get(k).get("id").toString(), lists.get(i).get("title").toString(), 
									lists.get(i).get("content").toString());
						}
					}
					if(scope.contains("银牌会员")){
						//查看会员类别列表
						List<Map<String, Object>> userList = userDao.selectRankUser("银牌会员");
						for(int k = 0;k < userList.size();k++){
							//AuroraPush.pushTargetForDriver(userList.get(k).get("id").toString(),lists.get(i).get("title").toString());
							//添加为系统消息
							int f = messageService.insertSystemMessage(userList.get(k).get("id").toString(), lists.get(i).get("title").toString(), 
									lists.get(i).get("content").toString());
						}
					}
					if(scope.contains("金牌会员")){
						//查看会员类别列表
						List<Map<String, Object>> userList = userDao.selectRankUser("金牌会员");
						for(int k = 0;k < userList.size();k++){
							//AuroraPush.pushTargetForDriver(userList.get(k).get("user_id").toString(),lists.get(i).get("title").toString());
							//添加为系统消息
							int f = messageService.insertSystemMessage(userList.get(k).get("id").toString(), lists.get(i).get("title").toString(), 
									lists.get(i).get("content").toString());
						}
					}
				}
			}
		}
	}
}
